var LockForm = new Class({
    Implements: [Options,Events],
    locked:false,
    formElement:null,
    options: { locked:      false,    //start status
               actions:     null,     //div with buttons
               toggle:      null,     //toggle button
               forever:     false,    //forever in locked state (true or false)
               resetOnLock: true,
               animate:     true,
               // Elements with class "nolock" will not be affected
               readonlyElements: 'input:not(.nolock):not(.readonly):not([type=hidden]), textarea',
               disabledElements: 'input:not(.nolock):not([type=text]):not([type=hidden]), button, select'
             },
    initialize: function(form, options) {
        this.setOptions(options);

        var forever = this.options.forever; //get state
        this.options.forever = false;

        this.formElement = form;
        this.formElement.instance_lockform = this;

        this.button = this.options.toggle;
        if (this.button) { 
            if (forever) {
                this.button.destroy();
                this.button = null;
            } else {
                this.button.addEvent('click',function() { this.toggle(); }.bind(this));
            }
        }

        if (forever && this.options.locked) {
            this.formElement.getElements(this.options.readonlyElements+','+this.options.disabledElements).addClass('noinput')
        }

        this.actions = $$(this.options.actions);
        if (this.actions) {
            if (forever && this.options.locked) {
                this.actions.destroy();
                this.actions = null;
            } else {
                if (this.options.animate) {
                    if (!this.actions.get('slide')) new Fx.Slide(this.actions);
                    this.actions.slide('hide');
                } else {
                    this.actions.hide();
                }
            }
        }
    
        if (this.options.locked) this.lock(); //lock @ start
        else this.unlock();

        this.options.forever = forever; //get cached state

    },

    /**/

    lock: function() {
        if (this.options.forever) return;
        this.locked = true;
        this.formElement.set('locked',true);

        //if (this.options.resetOnLock) this.formElement.reset();
        var inputElements = this.formElement.getElements(this.options.readonlyElements).set('readonly','readonly');
        var otherElements = this.formElement.getElements(this.options.disabledElements).set('disabled','disabled');

        this.actions = $$(this.options.actions);
        if (this.actions) 
            this.options.animate ? this.actions.slide('out') : this.actions.hide();

        if (this.button) {
            this.button.set('html','Edit');
            this.button.addClass('success');
            this.button.removeClass('danger');
        }

        this.fireEvent('lock');

    },

    unlock: function() {
        if (this.options.forever) return;
        this.locked = false;
        this.formElement.set('locked',false);

        var inputElements = this.formElement.getElements(this.options.readonlyElements).removeProperty('readonly');
        var otherElements = this.formElement.getElements(this.options.disabledElements).removeProperty('disabled');

        this.actions = $$(this.options.actions);
        if (this.actions)
            this.options.animate ? this.actions.slide('in') : this.actions.show();

        if (this.button) {
            this.button.set('html','Lock');
            this.button.removeClass('success');
            this.button.addClass('danger');
        }

        this.fireEvent('unlock');
    },

    toggle: function() {
        if (this.options.forever) return;
        if (this.locked) { this.unlock(); }
        else { this.lock(); }

    }
});
